#!/bin/bash
### BEGIN INIT INFO
# Provides:          lance_auto_channel
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      1
# Short-Description: Start lance tunnel
# Description: Start and Stop the lance_tool
### END INIT INFO
#
# Lance Tool
#
# chkconfig: 345 20 80
# description: Lance Tool
#
# /etc/rc.d/init.d/lance_*
# See how we were called.

BASE=`basename $0`
SSH="$HOME/ssh"

# The following are mapping config:
#>	LOCAL:2220	10.20.30.40:22
#>	LOCAL:2221	10.20.30.41:22
#>	LOCAL:2222	10.20.30.42:22
#>	LOCAL:2223	10.20.30.43:22
#>	LOCAL:2225	10.20.30.45:22
#>	LOCAL:2229	bl460c-g8-14.test:22
#>	LOCAL:3389	10.20.30.40:3389
#>	LOCAL:8079	10.20.30.41:80
#>	LOCAL:8088	10.20.30.42:8088


function time_stamp()
{
  date +%Y/%m/%d-%H:%M:%S
}

function read_config()
{
  cat $0 | grep "^#>" | awk -F"[\t: ]" '{print $3, $4, $5}'
}

function tunnel_is_opened()
{
  tunnel_name="$1"
  local_port="`echo $tunnel_name | awk -F: '{print $1}'`"

  netstat -antl | grep ":$local_port " 1>/dev/null 2>&1
  [ $? -ne 0 ] && echo "false" && return

  ps -elf | grep -v grep | grep "$tunnel_name" 1>/dev/null 2>&1
  [ $? -ne 0 ] && echo "false" && return

  echo "true" && return
}

function remote_reachable()
{
  remote_ip="$1"
  link_test=$(ping -c 3 "$remote_ip" | grep "time=")
  [ "$link_test" == "" ] && echo "false" && return
  echo "true" && return
}

function tunnel_open()
{
  tunnel_name="$1"
  remote_ip="`echo $tunnel_name | awk -F: '{print $2}'`"
  $SSH -Z $tunnel_name
  # Make sure you put secret in ~/.ssh/config
}

function tunnel_close()
{
  tunnel_name="$1"
  ps -elf | grep -v grep | grep "$tunnel_name" |\
  awk '{print $4}' | while read TPID
  do
    kill -9 "$TPID"
  done
}

function tunnel_one()
{
  [ "$1" == "" -o "$2" == "" -o "$3" == "" ] && return
  local_port="$1"
  remote_ip="$2"
  remote_port="$3"
  tunnel_name="$local_port:$remote_ip:$remote_port"

  [ "`tunnel_is_opened $tunnel_name`" == "true" ] && \
    echo "[`time_stamp`] "$local_port" port already opened!" && return

  [ "`remote_reachable $remote_ip`" == "false" ] && \
    echo "[`time_stamp`] "$local_port" port skipped! $remote_ip not reachable!" && return

  echo "[`time_stamp`] closing $tunnel_name......"
  tunnel_close $tunnel_name
  echo "[`time_stamp`] opening $tunnel_name......"
  tunnel_open  $tunnel_name
  return
}

function main_loop()
{
  read_config | while read local_port remote_ip remote_port
  do
    tunnel_one "$local_port" "$remote_ip" "$remote_port"
  done
}

case "$1" in
    one|start)
        echo "[`time_stamp`] run $BASE for once (ssh=$SSH)......"
        main_loop
        exit 0
    ;;

    start_bg)
        echo "`time_stamp`:Start $BASE ......"
        nohup $0 "background" 1>/var/log/$BASE.log 2>&1 &
        exit 0
    ;;

    stop)
        echo "Stop $BASE ......"
        ps -elf | grep -v grep | grep ssh | grep "\-Z" | awk '{print $4}' | while read TPID
        do
            kill -9 $TPID
        done
        exit 0
    ;;

    restart)
        $0 stop
        $0 start
        exit 0
    ;;

    status)
        echo "Status of $BASE:"
        ps -elf | grep -v grep | grep ssh | grep "\-Z"
        exit 0
    ;;

    background)
        while [ 1 == 1 ]
        do
            main_loop
            sleep 1800
        done
        exit 0
    ;;

    *)
        echo "Usage: /etc/init.d/`basename $0` {start|stop|restart|status}"
        exit 1
    ;;
esac


exit 0


